package com.atguigu.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/** WordCount - 无界流处理 - DataStream
        * 步骤:
        *   1. 创建执行环境
        *
        *   2. 从数据源读取数据
        *
        *   3. 对读取到的数据进行转换处理
        *
        *   4. 写出结果
        *
        *   5. 启动执行
        *
 * */

public class Flink03_UnBoundedStreamWordCount {
    public static void main(String[] args) {
        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        // 设置并行度为1
        env.setParallelism(1) ;
        //2.读取数据

        DataStreamSource<String> ds = env.socketTextStream("hadoop102", 9999);

        //3.对读取数据转换处理
        SingleOutputStreamOperator<Tuple2<String, Long>> flatMapds = ds.flatMap(
                new FlatMapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public void flatMap(String line, Collector<Tuple2<String, Long>> out) throws Exception {
                        String[] words = line.split(" ");
                        for (String word : words) {
                            out.collect(Tuple2.of(word,1L));
                        }
                    }
                }
        );
        //4.搜集结果
        KeyedStream<Tuple2<String, Long>, String> keyBy = flatMapds.keyBy(
                new KeySelector<Tuple2<String, Long>, String>() {
                    @Override
                    public String getKey(Tuple2<String, Long> value) throws Exception {

                        return value.f0;
                    }
                }
        );
        SingleOutputStreamOperator<Tuple2<String, Long>> sumDs = keyBy.sum(1);
        sumDs.print();
        //5.启动执行

        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
